<?php

/**
 * This is the model class for table "tbl_employeeequipment".
 *
 * The followings are the available columns in table 'tbl_employeeequipment':
 * @property integer $id
 * @property string $empno
 * @property string $mfg
 * @property string $model
 * @property string $serial
 * @property string $install
 * @property string $removed
 * @property string $warranty
 * @property string $seragrno
 * @property string $equiploc
 * @property string $notes
 * @property string $invoice
 * @property string $eqtype
 * @property string $plan
 * @property integer $status
 * @property string $image
 * @property integer $pmrequired
 */
class Employeeequipment extends CActiveRecord
{
    public $file;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Employeeequipment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_employeeequipment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('empno', 'required'),
			array('status, pmrequired', 'numerical', 'integerOnly'=>true),
			array('empno', 'length', 'max'=>4),
			array('mfg, install, removed, warranty', 'length', 'max'=>20),
			array('model', 'length', 'max'=>30),
			array('serial', 'length', 'max'=>50),
			array('seragrno, invoice', 'length', 'max'=>10),
			array('equiploc, plan, image', 'length', 'max'=>150),
			array('eqtype', 'length', 'max'=>25),
			array('notes', 'safe'),
                        array('file','file','types'=>'bmp,jpg,jpeg,gif,png','maxSize'=>10485760,'allowEmpty'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, empno, mfg, model, serial, install, removed, warranty, seragrno, equiploc, notes, invoice, eqtype, plan, status, image, pmrequired', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'empno' => 'Empno',
			'mfg' => 'Mfg',
			'model' => 'Model',
			'serial' => 'Serial',
			'install' => 'Install',
			'removed' => 'Removed',
			'warranty' => 'Warranty',
			'seragrno' => 'Seragrno',
			'equiploc' => 'Equiploc',
			'notes' => 'Notes',
			'invoice' => 'Invoice',
			'eqtype' => 'Eqtype',
			'plan' => 'Plan',
			'status' => 'Status',
			'image' => 'Image',
			'pmrequired' => 'Pmrequired',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('empno',$this->empno,true);
		$criteria->compare('mfg',$this->mfg,true);
		$criteria->compare('model',$this->model,true);
		$criteria->compare('serial',$this->serial,true);
		$criteria->compare('install',$this->install,true);
		$criteria->compare('removed',$this->removed,true);
		$criteria->compare('warranty',$this->warranty,true);
		$criteria->compare('seragrno',$this->seragrno,true);
		$criteria->compare('equiploc',$this->equiploc,true);
		$criteria->compare('notes',$this->notes,true);
		$criteria->compare('invoice',$this->invoice,true);
		$criteria->compare('eqtype',$this->eqtype,true);
		$criteria->compare('plan',$this->plan,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('pmrequired',$this->pmrequired);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function beforeSave() {
      
            
          
            $this->file=CUploadedFile::getInstance($this, 'file');
            if($this->file!=NULL){
                    //made a random name base on timestamp
                $ext=getFileExt($this->file);
                $name=getFileBodyName($this->file);


                $this->image=$name.'_'.time().$ext;

                $this->file->saveAs('images/content/'.$this->image,true);   
            }
            return parent::beforeSave();


        }
        public function beforeDelete() {
            parent::beforeDelete();
            if(file_exists('images/content/'.$this->image)){
                unlink('images/content/'.$this->image);
            }
            return true;
        }
        public function getPmrequiredText(){
            return isset($this->PmrequiredList[$this->pmrequired])?$this->PmrequiredList[$this->pmrequired]:NULL;
        }
        public function getPmrequiredList(){
            return array(
                0=>'No',
                1=>'Yes',
            );
        }
        public function getStatusText(){
            return isset($this->statusList[$this->status])?$this->statusList[$this->status]:NULL;
        }
        public function getStatusList(){
            return array(
                '-1'=>'Pending',
                '0'=>'Active',
                '1'=>'Removed',
            );
        }
        public function getPlanTypeText(){
            return isset($this->planTypeList[$this->plan])?$this->planTypeList[$this->plan]:NULL;
        }
        public function getPlanTypeList(){
            $data=Yii::app()->db->createCommand('select * from tbl_plantypes')->queryAll();
            if(count($data)>0){
                $rs=array();
                foreach($data as $i=>$item){
                    $rs[$item['id']]=$item['plantype'];
                }
                return $rs;
            }
            return NULL;
        }
}